package com.linglan.modules.hospital.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linglan.modules.hospital.dao.BizHospitalDao;
import com.linglan.modules.hospital.dto.result.HospitalResult;
import com.linglan.modules.hospital.entity.BizHospital;
import com.linglan.modules.hospital.service.BizHospitalService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 业务-医院表(BizHospital)表服务实现类
 *
 * @author makejava
 * @since 2025-05-04 22:21:10
 */
@Service("bizHospitalService")
public class BizHospitalServiceImpl extends ServiceImpl<BizHospitalDao, BizHospital> implements BizHospitalService {

    /**
     * 查询默认医院
     *
     * @return
     */
    @Override
    public HospitalResult findDefault() {
        LambdaQueryWrapper<BizHospital> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(BizHospital::getIsDeleted, 0);
        queryWrapper.eq(BizHospital::getIfDefault, 1);
        List<BizHospital> list = this.list(queryWrapper);
        if (CollectionUtils.isNotEmpty(list)) {
            return getHospitalResult(list);
        }
        return null;
    }

    /**
     * 根据id查询医院详情
     *
     * @param id
     * @return
     */
    @Override
    public HospitalResult findById(Long id) {
        LambdaQueryWrapper<BizHospital> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(BizHospital::getIsDeleted, 0);
        queryWrapper.eq(BizHospital::getId, id);
        List<BizHospital> list = this.list(queryWrapper);
        if (CollectionUtils.isNotEmpty(list)) {
            return getHospitalResult(list);
        }
        return null;
    }

    /**
     * 批量查询医院详情
     *
     * @param ids
     * @return
     */
    @Override
    public Map<Long, HospitalResult> findGroupById(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return new HashMap<>();
        }
        LambdaQueryWrapper<BizHospital> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(BizHospital::getIsDeleted, 0);
        if (CollectionUtils.size(ids) == 1) {
            queryWrapper.eq(BizHospital::getId, ids.get(0));
        } else {
            queryWrapper.in(BizHospital::getId, ids);
        }
        List<BizHospital> list = this.list(queryWrapper);
        if (CollectionUtils.isNotEmpty(list)) {
            Map<Long, HospitalResult> map = new HashMap<>();
            for (BizHospital hospital : list) {
                map.put(hospital.getId(), getHospitalResult(list));
            }
            return map;
        }
        return new HashMap<>();
    }

    private static HospitalResult getHospitalResult(List<BizHospital> list) {
        BizHospital record = list.get(0);
        HospitalResult result = new HospitalResult();
        result.setId(record.getId());
        result.setName(record.getName());
        result.setAddress(record.getAddress());
        result.setLat(record.getLat());
        result.setLng(record.getLng());
        return result;
    }
}

